package com.szholly.pro.watergas_server.common.util;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.freemarker.directive.DirectiveUtils;
import com.szholly.utils.freemarker.directive.IDirective;
import com.szholly.utils.spring.springmvc.route.FunctionInfo;
import org.springframework.stereotype.Component;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 定位器
 */
@Component("directive_RiverAreaSelect")
public class RiverAreaSelectDirective implements TemplateDirectiveModel, IDirective {
	@Override
	public FunctionInfo getFunctionInfo() {
		String paraInfo = "tableName：数据字典表名 <br/>order：排序字段<br/>jbs：需要加载的级别"
				+ "调用示例：<br/>"
				+ "<@locator><br/>"
				+ "<#if data??> <br/>" + "<#list data as item><br/>"
				+ "<li>${item.C_ORGNAME}</li><br/>" + "</#list><br/>"
				+ "</#if><br/>" + "</@locator><br/>";

		FunctionInfo info = new FunctionInfo("treedata", 1, "表单",
				"获取树型下拉框的字典数据", paraInfo);
		return info;
	}
	
	static List<RowBase> riverMap = new ArrayList<>();

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void execute(Environment env, Map params, TemplateModel[] model,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		if(riverMap == null || riverMap.size()==0){
			String sql = " select S_DOMAINCODE,S_DOMAINCAPTION from DATA_DEFULATDOMAIN where s_domainname = 'STORM_SYSTEM' ";
			riverMap = DataUtil.getSelectRows(sql, null);
		}
		
		Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
				params);
		paramWrap.put("data", DEFAULT_WRAPPER.wrap(riverMap));
		
		DirectiveUtils.addParamsToVariable(env, paramWrap);
		body.render(env.getOut());
	}
}